package com.green.web.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.green.dao.cache.XMemcachedCache;
import com.green.model.manage.Area;
import com.green.model.manage.AreaPermission;
import com.green.model.manage.FunPermission;
import com.green.model.manage.Module;
import com.green.model.manage.Org;
import com.green.model.manage.Person;
import com.green.model.manage.Resources;
import com.green.service.common.ServiceContext;
import com.green.service.common.SpringUtil;

/**
 * 权限辅助类
 * 
 * @author fengwenjin
 * 
 */
public class PermissionHelper {

	private static final String FUN_PERMISSION_KEY = "FUN_PERMISSION_KEY";// 功能权限KEY
	private static final String DATA_PERMISSION_KEY = "DATA_PERMISSION_KEY";// 数据权限KEY
	private static final String AREA_PERMISSION_KEY = "AREA_PERMISSION_KEY";// 区域权限KEY
	private static final XMemcachedCache memcachedClient = SpringUtil.getBean("xMemcachedCache");
	private static final int CACHE_TIME = 3600 * 24;

	@SuppressWarnings("unchecked")
	public static Map<String, String> getOperateCodes() {
		Map<String, String> codes = new HashMap<String, String>();
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			List<FunPermission> list = (List<FunPermission>) memcachedClient.get(person.getId() + FUN_PERMISSION_KEY);
			if (list != null && list.size() > 0) {
				for (FunPermission permission : list) {
					Module module = permission.getModule();
					if (module != null && module.getCode() != null) {
						codes.put(module.getCode(), module.getCode());
						continue;
					}
					Resources resource = permission.getResource();
					if (resource != null && null != resource.getCode()) {
						codes.put(resource.getCode(), resource.getCode());
					}
				}
			}
		}
		return codes;
	}

	@SuppressWarnings("unchecked")
	public static boolean hasPermission(String url) {
		if (StringUtils.isEmpty(url)) {
			return false;
		}
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			List<FunPermission> list = (List<FunPermission>) memcachedClient.get(person.getId() + FUN_PERMISSION_KEY);
			if (list != null && list.size() > 0) {
				for (FunPermission permission : list) {
					Module module = permission.getModule();
					if (module != null && module.getUrl() != null) {
						String[] strs = StringUtils.split(module.getUrl(), ",");
						for (String s : strs) {
							if (url.contains(s)) {
								return true;
							}
						}
					}
					Resources resource = permission.getResource();
					if (resource != null && resource.getUrl() != null) {
						String[] strs = StringUtils.split(resource.getUrl(), ",");
						for (String s : strs) {
							if (url.contains(s)) {
								return true;
							}
						}
					}
				}
			}
		}
		return false;
	}

	@SuppressWarnings("unchecked")
	public static List<String> getOrgIds() {
		List<String> orgIds = new ArrayList<String>();
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			orgIds = (List<String>) memcachedClient.get(person.getId() + DATA_PERMISSION_KEY);
		}
		return orgIds;
	}

	/**
	 * 返回数据权限组织ID拼接好的字符串 '1','2','3'
	 * 
	 * @return
	 */
	public static String getOrgIdsStr() {
		List<String> permissionOrgList = getOrgIds();
		if (permissionOrgList != null && permissionOrgList.size() > 0) {
			int i = 0;
			StringBuilder permissionOrgIds = new StringBuilder("");
			for (String orgId : permissionOrgList) {
				permissionOrgIds.append("'" + orgId + "'");
				if (i < permissionOrgList.size() - 1) {
					permissionOrgIds.append(",");
				}
				i++;
			}
			return permissionOrgIds.toString();
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public static List<String> getCitys() {
		List<String> citys = new ArrayList<String>();
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			List<AreaPermission> list = (List<AreaPermission>) memcachedClient.get(person.getId() + AREA_PERMISSION_KEY);
			if (list != null && list.size() > 0) {
				Area area;
				for (AreaPermission permission : list) {
					area = permission.getArea();
					if (area.getLevel() == 2) {
						citys.add(area.getName());
					}
				}
			}
		}
		return citys;
	}

	public static void setFunPermissionList(List<FunPermission> list) {
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			memcachedClient.set(person.getId() + FUN_PERMISSION_KEY, CACHE_TIME, list);
		}
	}

	public static void setDataPermissionList(List<Org> list) {
		if (list != null && list.size() > 0) {
			HashSet<String> orgIds = new HashSet<String>();
			for (Org org : list) {
				orgIds.add(org.getId());
			}
			Person person = ServiceContext.getLoginPerson();
			if (person != null) {
				memcachedClient.set(person.getId() + DATA_PERMISSION_KEY, CACHE_TIME, new ArrayList<String>(orgIds));
			}
		}
	}

	public static void setAreaPermissionList(List<AreaPermission> list) {
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			memcachedClient.set(person.getId() + AREA_PERMISSION_KEY, CACHE_TIME, list);
		}
	}

	public static void inValidate() {
		Person person = ServiceContext.getLoginPerson();
		if (person != null) {
			String id = person.getId();
			memcachedClient.delete(id + FUN_PERMISSION_KEY);
			memcachedClient.delete(id + DATA_PERMISSION_KEY);
			memcachedClient.delete(id + AREA_PERMISSION_KEY);
		}
	}

}
